构建环境
- mac os x
- 系统:Mac OS Catalina:10.15 (19A583)
- 构建的jdk版本:openjdk 8u
- Bootstrap JDK:1.8.0_181
- /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
- Xcode:11
- CPU:Intel Core i7 2.2GHz 6核
- 内存:16GB 2400 MHz
构建步骤
1.Github openJDK地址
https://github.com/unofficial-openjdk/openjdk
2.克隆完成后,使用脚本获取最新的源代码
cd jdk8u-dev
sh ./get_source.sh #
根据网络环境的不同,这一步花费的时间不同,一般花费时间较长
3.当然,还需要准备一些必须的工具,比如安装最新版的 Xcode,有g++,gcc 的环境,安装 freetype, 使用命令brew install freetype进行安装
4.设置环境变量 ~/.bash_profile
查看安装的java环境
/usr/libexec/java_home -V
Matching Java Virtual Machines (1):
1.8.0_181, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
建议建立一个sh脚本
# 设定语言选项,必须设置
export LANG=C
# Mac平台,C编译器不再是GCC,是clang
export CC=clang
# 跳过clang的一些严格的语法检查,不然会将N多的警告作为Error
export COMPILER_WARNINGS_FATAL=false
# 链接时使用的参数
export LFLAGS='-Xlinker -lstdc++'
# 是否使用clang
export USE_CLANG=true
# 使用64位数据模型
export LP64=1
# 告诉编译平台是64位,不然会按32位来编译
export ARCH_DATA_MODEL=64
# 允许自动下载依赖
export ALLOW_DOWNLOADS=true
# 并行编译的线程数,编译时间长,为了不影响其他工作,我选择为6
export HOTSPOT_BUILD_JOBS=6
# 是否跳过与先前版本的比较
export SKIP_COMPARE_IMAGES=true
# 是否使用预编译头文件,加快编译速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量编译
export INCREMENTAL_BUILD=true
# 编译内容
export BUILD_LANGTOOLS=true
export BUILD_JAXP=true
export BUILD_JAXWS=true
export BUILD_CORBA=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true
# 编译版本
export SKIP_DEBUG_BUILD=true
export SKIP_FASTDEBUG_BUILD=false
export DEBUG_NAME=debug
# 避开javaws和浏览器Java插件之类的部分的build
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
unset JAVA_HOME
unset CLASSPATH
make 2>&1|tee $ALT_OUTPUTDIR/build.log
之后使用source ~/.bash_profile 生效
确保本地有现有的 java 环境,使用java -version 进行查看
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
5.开始configure, 我这里用到的 configure 命令为如下,防止找不到 freetype的路径
bash ./configure --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home --with-freetype-include=/opt/X11/include/freetype2/ --with-freetype-lib=/opt/X11/lib --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Users/yupingtao/dev/c++_workspace/xcode-missing-libstdc-/include/c++/4.2.1
编译完成
Tools summary:
* Boot JDK: java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home)
* C Compiler: Apple clang version (clang-1100.0.33.8) version Apple clang version 11.0.0 (clang-1100.0.33.8) (at /usr/bin/clang)
* C++ Compiler: version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 (at /usr/bin/g++)
Build performance summary:
* Cores to use: 5
* Memory limit: 16384 MB
* ccache status: installed, but disabled (version older than 3.1.4)
WARNING: You have old-style ALT_ environment variables set.
These are not respected, and will be ignored. It is recommended
that you clean your environment. The following variables are set:
ALT_PARALLEL_COMPILE_JOBS=2
WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems
6.开始make all, 真正开始编译
----- Build times -------
Start 2019-10-18 23:05:44
End 2019-10-18 23:15:55
00:00:18 corba
00:00:31 demos
00:01:26 docs
00:01:38 hotspot
00:01:24 images
00:00:14 jaxp
00:00:23 jaxws
00:03:47 jdk
00:00:19 langtools
00:00:10 nashorn
00:10:11 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
可能遇到的问题
Q1 could not find freetype
configure: error: Could not find freetype!
configure exiting with result code 1
configure: error: Can not find or use freetype at location given by --with-freetype
解决方法:通过安装XQuartz来安装freetype,然后执行configure时加上选项--with-freetype-include=/opt/X11/include/freetype2/ --with-freetype-lib=/opt/X11/lib
Q2 错误:
...
checking flags for boot jdk java command ...
checking flags for boot jdk java command for big workloads... -Xms64M -Xmx1600M -XX:ThreadStackSize=1536 -XX:PermSize=32m -XX:MaxPermSize=160m
checking flags for boot jdk java command for small workloads... -XX:+UseSerialGC -Xms32M -Xmx512M
checking for jtreg... no
checking Determining if we need to set DEVELOPER_DIR... no
checking for xcodebuild... /usr/bin/xcodebuild
configure: error: Xcode 4 is required to build JDK 8, the version found was 9.2. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select.
configure exiting with result code 1
解决方法: 找到configure 文件并打开vim common/autoconf/generated-configure.sh,找到判断版本的地方,将这一段全部注释掉.
# Fail-fast: verify we're building on Xcode 4, we cannot build with Xcode 5 or later
XCODE_VERSION=`$XCODEBUILD -version | grep '^Xcode ' | sed 's/Xcode //'`
XC_VERSION_PARTS=( ${XCODE_VERSION//./ } )
if test ! "${XC_VERSION_PARTS[0]}" = "4"; then
as_fn_error $? "Xcode 4 is required to build JDK 8, the version found was $XCODE_VERSION. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select." "$LINENO" 5
fi
继续编译. Q3 找不到gcc 编译器
...
checking for xcodebuild... /usr/bin/xcodebuild
checking Determining Xcode SDK path... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
checking for clang... /usr/local/opt/llvm@4/bin/clang
configure: Resolving CC (as /usr/local/opt/llvm@4/bin/clang) failed, using /usr/local/opt/llvm@4/bin/clang directly.
checking resolved symbolic links for CC... /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0
checking if CC is disguised ccache... no, keeping CC
configure: The C compiler (located as /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0) does not seem to be the required GCC compiler.
configure: The result from running with --version was: "clang version 4.0.1 (tags/RELEASE_401/final)"
configure: error: GCC compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1
解决方案:
原因:从XCode 4.2开始,Clang已经取代GCC成为macOS的默认的编译器:http://clang.llvm.org/get_started.html
解决过程(不想换jdk9或者装旧版XCode ):
没有GCC,那就安装一个呗(可以选择注释掉GCC compiler 报错的代码,这样就可以不必安装gcc,并用clang编译),不要安装编译器版本高于5的,因为默认启用c++14 导致编译中断,我选择安装的是gcc4.9:
brew install gcc@4.9
安装完需要把安装的gcc4.9指向系统默认调的gcc:
sudo ln -s /usr/local/Cellar/gcc@4.9/4.9.4_1/bin/gcc-4.9 /usr/bin/gcc
sudo ln -s /usr/local/Cellar/gcc@4.9/4.9.4_1/bin/g++-4.9 /usr/bin/g++
Q4 libc错误
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
原因:Xcode 10 以后移除了 libstdc++ 的支持,而 hotspot 这个辣鸡居然还一直用着这个,于是编译器找不到 libstdc++ 的头文件就罢工了
解决办法:
打开 这个链接, clone 到本地,参考 install.sh 将文件链接或者复制到对应位置(慎重直接执行,请一定事先核对路径是否正确!)
https://github.com/imkiwa/xcode-missing-libstdc-
Q5 #include 头文件缺失
这是由于xcode9升级到xcode10过后删除了一些文件导致的,只需要把xcode9的lib包导入即可
和上面的问题实际上是一个问题,重新设置c的运行库
打开用户目录下的 .bash_profile 文件,添加如下内容:
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
export LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
Q6 错误
/Users/xixfeng/ly_jvm/openjdk/hotspot/src/share/vm/gc/g1/heapRegionSet.hpp:53:38: note: expanded from macro ‘HEAP_REGION_SET_FORCE_VERIFY’
// Set verification will be forced either if someone defines
// HEAP_REGION_SET_FORCE_VERIFY to be 1, or in builds in which
// asserts are compiled in.如果这个值被定义或者在编译断言的声明中,执行强制检验
#ifndef HEAP_REGION_SET_FORCE_VERIFY
#define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT)
#endif // HEAP_REGION_SET_FORCE_VERIFY
#if HEAP_REGION_SET_FORCE_VERIFY
void verify_optional() {
verify();
}
#else // HEAP_REGION_SET_FORCE_VERIFY
void verify_optional() { }
#endif // HEAP_REGION_SET_FORCE_VERIFY
将defined(ASSERT) 改为 0 Q7 异常
if (Universe::narrow_oop_base() > 0) { // Implies UseCompressedOops.
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
1 error generated.
改为
if (Universe::narrow_oop_base() != NULL) {
Q8 异常
virtualspace.cpp:584:14: error: ordered comparison between pointer and zero ('char *' and 'int')
if (base() > 0) {
改为
base() != 0